package com.zzty.txj1.controller;

import com.zzty.txj1.entity.Location;
import com.zzty.txj1.service.LocationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/location")
public class LocationController {
    private static final Logger logger = LoggerFactory.getLogger(LocationController.class);
    @Autowired
    private LocationService locationService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Map add(Location location){
        logger.info("location--controller ============ add");
        Map<String,String> result=new HashMap<>();
        String resultValue="false";
        Integer count=locationService.addLocation(location);
        if(count==1){
            resultValue="success";
        }
        result.put("msg",resultValue);
        return result;
    }

    @RequestMapping("/getAll")
    public List getAll(){
        logger.info("location--controller ============ getAll");
        List<Location> locations=locationService.getAllLocation();
        return locations;
    }
    @RequestMapping("/getLocationById")
    public List getLocationById(Location location){
        logger.info("location--controller ============ getLocationById"+"|id="+location.getId());
        Location location1=locationService.getLocationById(location);
        List<Location> locations=new ArrayList<>();
        locations.add(location1);
        return locations;
    }
    @RequestMapping(value = "/deleteLocationById",method = RequestMethod.POST)
    public Map deleteLocationById(Location location){
        logger.info("location--controller ============ deleteLocationById"+"|id="+location.getId());
        Map<String,String> results=new HashMap<>();
        String result="false";
        Integer count=locationService.deleteLocationById(location);
        if(count==1){
            result="success";
        }
        results.put("msg",result);
        return results;
    }
    @RequestMapping(value = "/updateLocationById",method = RequestMethod.POST)
    public Map updateLocationById(Location location){
        logger.info("location--controller ============ updateLocationById"+"|id="+location.getId());
        Map<String,String> results=new HashMap<>();
        String result="false";
        Integer count=locationService.updateLocationById(location);
        if(count==1){
            result="success";
        }
        results.put("msg",result);
        return results;
    }

}
